package com.example;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.FileType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Properties;

/**
 * 生成Mapper映射
 */
public class MybatisPlusGenerator {
    public MybatisPlusGenerator() {
    }

    public static void generator() throws IOException {
        AutoGenerator mpg = new AutoGenerator();
        final GlobalConfig gc = new GlobalConfig();
        final String projectPath = Thread.currentThread().getContextClassLoader().getResource("").getPath().replaceAll("/target/.*/", "");
        final Properties properties = new Properties();
        InputStream in = new FileInputStream(new File(projectPath, "src/main/resources/dbconfig.properties"));
        properties.load(in);
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor(properties.getProperty("author"));
        gc.setOpen(false);
        gc.setBaseResultMap("true".equals(properties.getProperty("baseResultMap")));
        gc.setFileOverride("true".equals(properties.getProperty("file.override")));
        gc.setBaseColumnList("true".equals(properties.getProperty("baseColumnList")));
        gc.setSwagger2("true".equals(properties.getProperty("swagger2")));
        gc.setDateType(DateType.ONLY_DATE);
        mpg.setGlobalConfig(gc);
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(properties.getProperty("db6.jdbc.url"));
        dsc.setDriverName(properties.getProperty("mysql.jdbc.driver"));
        dsc.setUsername(properties.getProperty("db6.jdbc.username"));
        dsc.setPassword(properties.getProperty("db6.jdbc.password"));
        mpg.setDataSource(dsc);
        final PackageConfig pc = new PackageConfig();
        pc.setModuleName(properties.getProperty("moulde.name"));
        pc.setParent(properties.getProperty("parent.package"));
        mpg.setPackageInfo(pc);
        InjectionConfig cfg = new InjectionConfig() {
            public void initMap() {
            }
        };
        String templatePath = "/templates/mapper.xml.ftl";
        List<FileOutConfig> focList = new ArrayList();
        focList.add(new FileOutConfig(templatePath) {
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + ".xml";
            }
        });
        cfg.setFileCreate(new IFileCreate() {
            public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
                if (fileType.equals(FileType.CONTROLLER) && !Objects.equals("true", properties.getProperty("generateController"))) {
                    return false;
                } else {
                    File file = new File(filePath);
                    boolean exist = file.exists();
                    if (!exist) {
                        file.getParentFile().mkdirs();
                    }

                    return !exist || gc.isFileOverride();
                }
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperEntityClass(properties.getProperty("super.entity.class"));
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setLogicDeleteFieldName(properties.getProperty("logicDeleteFieldName"));
        strategy.setVersionFieldName(properties.getProperty("versionFieldName"));
        strategy.setSuperControllerClass(properties.getProperty("super.controller.class"));
        strategy.setInclude(properties.getProperty("tables").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        if ("true".equals(properties.getProperty("removeModulePrefix"))) {
            strategy.setTablePrefix(pc.getModuleName() + "_");
        }

        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

    public static void main(String[] args) throws IOException {
        generator();
    }
}
